/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com This program is free
 * software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

import de.mordred.Frage;

public class AntwortenPanel extends JPanel
{

	private static final long serialVersionUID = -6922564602222417406L;

	boolean _istPruefung;
	FButton[] _buttons = new FButton[5];

	public AntwortenPanel(boolean pruefung)
	{
		_istPruefung = pruefung;
		setLayout(new GridLayout(5, 1));
	}

	public void setAntworten(final Frage frage)
	{
		removeAll();

		for (int i = 0; i < frage.getAntworten().length; i++)
		{
			final boolean richtig = frage.getRichtigeantwort() == i;
			final FButton fb;

			if (frage.hatAntwortBilder())
			{
				fb = new FButton(frage.getAntworten()[i], frage.getBildFuerAntwort(i));
			}
			else
			{
				fb = new FButton(frage.getAntworten()[i]);
			}

			_buttons[i] = fb;
			add(_buttons[i]);

			/**
			 * Hier werden alte Antworten beim zurückgehen Markiert
			 */
			if (frage.getSelectedAntwort() == i)
			{
				answerSelected(richtig, fb);
			}

			final int ii = i;
			fb.addMouseListener(new MouseAdapter()
			{
				@Override
				public void mouseClicked(MouseEvent e)
				{
					answerSelected(richtig, fb);
					frage.selectAntwort(ii);
				}
			});
		}
		this.repaint();
		this.revalidate();
	}

	private void answerSelected(boolean richtig, FButton fb)
	{

		for (FButton fbb : _buttons)
		{
			fbb.setNormal();
		}

		if (_istPruefung)
		{
			fb.setSelected();
		}
		else
		{
			if (richtig)
				fb.rightAnswer();
			else
				fb.wrongAnswer();
		}
	}

	public void setButtonsEnabled(boolean enabled)
	{
		for (FButton fb : _buttons)
		{
			fb.setEnabled(enabled);
		}
	}

}
